package com.yixin.ystartlibrary.helper;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.util.HashMap;

public class FragmentHelper {

    private FragmentManager mFragmentManager;
    private HashMap<String, FragmentInfo> mFragmentItems = new HashMap<>();
    private FragmentInfo mShowingInfo;
    private int mFrameRes;
    private Context mContext;

    public static class FragmentInfo {
        private String mTag;
        private Fragment mFragment;
        private boolean isAdd;

        public FragmentInfo(@NonNull Fragment fragment) {
            mTag = fragment.getClass().getSimpleName();
            mFragment = fragment;
        }

        public FragmentInfo(String tag, @NonNull Fragment fragment) {
            mTag = tag;
            mFragment = fragment;
        }

        public Fragment getFragment() {
            return mFragment;
        }

        public String getTag() {
            return mTag;
        }
    }

    public FragmentHelper(Context context, FragmentManager fragmentManager, int frameRes) {
        mContext = context;
        this.mFragmentManager = fragmentManager;
        this.mFrameRes = frameRes;

        mFragmentItems.clear();
    }

    /**
     * Add fragment info for map.
     *
     * @param info FragmentInfo
     */
    public void addFragmentItem(FragmentInfo info) {
        mFragmentItems.put(info.getTag(), info);
    }

    /**
     * Show fragment by tag.
     *
     * @param tag String
     */
    public void show(String tag) {
        show(mFragmentItems.get(tag));
    }


    /**
     * Judge the tag's fragment is showing.
     *
     * @param tag String
     * @return String
     */
    public boolean isShowing(String tag) {
        if (mShowingInfo.mTag.equals(tag)) {
            return true;
        }
        return false;
    }

    /**
     * Show fragment by info.
     *
     * @param info FragmentInfo
     */
    public void show(FragmentInfo info) {

        final FragmentTransaction trans = mFragmentManager.beginTransaction()
                .disallowAddToBackStack();

        if (mShowingInfo == info) {
            // Fragment is showing, don't try
            // again
        } else {
            if (mShowingInfo != null) {
                // Detach showing fragment
                if (mShowingInfo.mFragment != null) {
                    trans.hide(mShowingInfo.mFragment);
                }
            }
            mShowingInfo = info;
            if (mShowingInfo != null) {
                // Attach init fragment
                if (!mShowingInfo.isAdd) {
                    mShowingInfo.isAdd = true;
                    trans.add(mFrameRes, mShowingInfo.mFragment, mShowingInfo.mTag);
                } else {
                    trans.show(mShowingInfo.mFragment);
                }
            }
        }

        if (!trans.isEmpty()) {
//            trans.commit();
            trans.commitAllowingStateLoss();
        }
    }
}
